<?php
/**
 * Created by yb.
 * Author: 1021948372@qq.com
 * Date: 2020/7/7 07:07
 */
namespace app\common\controller;

use app\common\lib\SessionCheck;

class AdminRole extends SessionCheck
{
    /**
     * 列表
     * @return mixed
     */
    public function index()
    {
        return $this->fetch('common@admin_role/index');
    }

    public function getDataList(){
        $result = model('AdminRole')->getPageListData('*',array('module' => request()->module()),'create_time desc');
        $this->success('操作成功','',$result);
    }

    /**
     * 添加
     * @return mixed
     */
    public function add(){
        if(request()->isPost()){
            if(empty(input('name'))){
                $this->error('请填写名称');
            }
            $saveData = array(
                'module' => request()->module(),
                'name' => trim(input('name')),
                'menu_ids' => input('ids') ? implode(',',input('ids')) : '',
                'status' => 1,
                'remark' => input('remark',''),
                'create_time' => time()
            );
            $model = new \app\common\model\AdminRole();
            if($model->insert($saveData)){
                $this->success('操作成功');
            }else{
                $this->error('操作失败');
            }
        }else{
            //权限列表
            $menuList = db('admin_menu')->where(array('module' => request()->module()))->select();
            $menuList = $this->genTree($menuList);
            $this->assign('menuList',$menuList);
            return $this->fetch('common@admin_role/add');
        }
    }

    /**
     * 修改
     * @return mixed
     */
    public function edit(){
        if(request()->isPost()){
            if(empty(input('name'))){
                $this->error('请填写名称');
            }
            $id = input('id');
            $saveData = array(
                'name' => trim(input('name')),
                'menu_ids' => input('ids') ? implode(',',input('ids')) : '',
                'remark' => input('remark','')
            );
            $model = new \app\common\model\AdminRole();
            if($model->save($saveData,['id' => $id])){
                $this->success('操作成功');
            }else{
                $this->error('操作失败');
            }
        }else{
            $id = input('id');
            $data = db('admin_role')->where(array('id' => $id))->find();
            //获取权限数据
            $menuList = db('admin_menu')->where(array('module' => request()->module()))->select();
            $menuList = $this->genTree($menuList);
            $selectedMenu = $data['menu_ids'] ? explode(',',$data['menu_ids']) : array();
            foreach ($menuList as $k => &$item){
                if(in_array($item['id'],$selectedMenu)){
                    $item['checked'] = 1;
                }else{
                    $item['checked'] = 0;
                }
                if(isset($item['children']) && is_array($item['children'])){
                    foreach ($item['children'] as $kk => &$v){
                        if(in_array($v['id'],$selectedMenu)){
                            $v['checked'] = 1;
                        }else{
                            $v['checked'] = 0;
                        }
                    }
                }
            }
            $this->assign('menuList', $menuList);
            $this->assign('data', $data);
            return $this->fetch('common@admin_role/edit');
        }
    }

    /**
     * 修改状态
     */
    public function editStatus(){
        $id = input('id',0);
        $status = input('status',1);
        $saveData = array(
            'status' => $status
        );
        $model = new \app\common\model\AdminRole();
        if($model->save($saveData,['id'=>$id])){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
    }

    /**
     * 删除
     */
    public function del(){
        $id = input('id',0);
        $model = new \app\common\model\AdminRole();
        if($model->where(array('id' => $id))->delete()){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

}